<?php

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

use think\facade\Route;
use app\http\middleware\CheckAuth;

/**
 * 动词规范
 * get 获取单个，返回单个对象
 * getAll 获取全部，返回数组
 * query 按条件查询，返回分页
 * fetch 获取全部，返回分页
 * create 创建新对象
 * edit 修改对象
 * remove 删除对象
 * 其他 按实际含义来
 */

Route::get('/', 'index/Index/index');

Route::group('api/v1', function () { // TODO
    // 系统版本
    Route::get('version', 'index/Index/version');
    // 依赖检查
    Route::get('check', 'index/Index/check');
    // 聚合接口
    Route::post('mixed', 'index/Index/mixedRequests');
    // 登录相关
    Route::group('auth', function () {
        Route::get('publickey', 'index/Auth/getPublickey');
        Route::get('current', 'index/Auth/getCurrentUser');
        Route::post('login', 'index/Auth/login');
        Route::post('avatar', 'index/Auth/uploadAvatar');
        Route::delete('logout', 'index/Auth/logout');
        Route::get('logout', 'index/Auth/logout');
        Route::put('password', 'index/Auth/changeMyPassword');
    });
    // 需要登录的接口
    Route::group('', function () {
        // 用户相关
        Route::group('users', function () {
            // 用户管理
            Route::group('', function () {
                Route::get('/$', 'index/Users/queryUsers');
                Route::get('/:id$', 'index/Users/getUser'); // 未测试
                Route::post('/$', 'index/Users/createUser');
                Route::put('/:id$', 'index/Users/editUser');
                Route::put('/:id/password$', 'index/Users/changeUserPassword');
                Route::delete('/:id$', 'index/Users/removeUser');
            })->middleware(CheckAuth::class, 'users.management');
            // 角色权限信息
            Route::group('', function () {
                Route::get('permissions$', 'index/Users/getAllPermissions');
                Route::get('roles$', 'index/Users/getAllRoles');
            })->middleware(CheckAuth::class, 'info.public');
        });
        // 题目类型管理
        Route::group('types', function () { // 无UI
            Route::get('/$', 'index/Types/getAllTypes')->middleware(CheckAuth::class, 'info.public');
            Route::group('', function () {
                Route::post('/$', 'index/Types/createType');
                Route::put('/:id$', 'index/Types/editType');
                Route::delete('/:id$', 'index/Types/removeType');
                Route::get('consistent$', 'index/Types/checkConsistent');
                Route::put('consistent$', 'index/Types/fixConsistent');
            })->middleware(CheckAuth::class, 'types.management');
        });
        // 题目
        Route::group('questions', function () {
            // 出题
            Route::group('', function () {
                Route::get('/$', 'index/Questions/queryQuestions');
                Route::get('/:id$', 'index/Questions/getQuestion');
                Route::post('/$', 'index/Questions/createQuestion');
                Route::post('/:id/image$', 'index/Questions/uploadImage');
                Route::put('/:id$', 'index/Questions/editQuestion');
                Route::delete('/:id$', 'index/Questions/removeQuestion');
            })->middleware(CheckAuth::class, 'questions.post');
            // 审题
            Route::get('/next-check$', 'index/Questions/getRandomUncheckedQuestion')->middleware(CheckAuth::class, 'questions.check');
            Route::put('/:id/check$', 'index/Questions/checkQuestion')->middleware(CheckAuth::class, 'questions.check');
            // 刷题
            Route::get('/query-exam$', 'index/Questions/queryQuestionsWithExam')->middleware(CheckAuth::class, 'questions.exam');
            Route::get('/next-exam$', 'index/Questions/getNextUnexamedQuestion')->middleware(CheckAuth::class, 'questions.exam');
            Route::put('/:id/exam$', 'index/Questions/examQuestion')->middleware(CheckAuth::class, 'questions.exam');
            // 题目留言
            Route::group('/:qid/comments', function () {
                Route::get('/$', 'index/MessagesComment/fetchQuestionCommentMessages');
                Route::post('/$', 'index/MessagesComment/createQuestionCommentMessage');
                Route::put('/:id$', 'index/MessagesComment/editQuestionCommentMessage');
                Route::delete('/:id$', 'index/MessagesComment/removeQuestionCommentMessage');
            })->middleware(CheckAuth::class, 'questions.comment')->pattern('qid', '\d+');
            // 统计数据
            Route::get('/statistics$', 'index/Questions/getQuestionStatistics')->middleware(CheckAuth::class, 'info.public');
            Route::get('/statistics_author$', 'index/Questions/getQuestionStatisticsByAuthor')->middleware(CheckAuth::class, 'questions.statistics_author');
        });
        // 消息相关
        Route::group('messages', function () {
            // 私信
            Route::group('private', function () {
                Route::get('/count', 'index/MessagesPrivate/getPrivateMessageCount');
                Route::get('/contacts$', 'index/MessagesPrivate/getAllContatcs');
                Route::get('/chat-list$', 'index/MessagesPrivate/fetchChatList');
                Route::get('/chat-list/:right_id', 'index/MessagesPrivate/fetchPrivateMessages');
                Route::get('/:id$', 'index/MessagesPrivate/getPrivateMessages');
                Route::post('/$', 'index/MessagesPrivate/createPrivateMessage');
                Route::delete('/:id$', 'index/MessagesPrivate/removePrivateMessage');
            })->middleware(CheckAuth::class, 'messages.private');
            // 留言板
            Route::group('board', function () {
                Route::get('/$', 'index/MessagesBoard/fetchBoardMessages');
                Route::post('/$', 'index/MessagesBoard/createBoardMessage');
                Route::put('/:id$', 'index/MessagesBoard/editBoardMessage');
                Route::delete('/:id$', 'index/MessagesBoard/removeBoardMessage');
            })->middleware(CheckAuth::class, 'messages.private');
            // 复审消息
            Route::group('recheck-comments', function () {
                Route::get('/$', 'index/MessagesComment/fetchRecheckCommentMessages');
                Route::put('/:id', 'index/MessagesComment/editRecheckCommentMessage');
            })->middleware(CheckAuth::class, 'questions.check');
            // 公告
            Route::group('notice', function () {
                Route::get('/$', 'index/MessagesNotice/getAllNoticeMessages')->middleware(CheckAuth::class, 'info.public');
                Route::group('', function () {
                    Route::post('/$', 'index/MessagesNotice/createNoticeMessage');
                    Route::put('/:id$', 'index/MessagesNotice/editNoticeMessage');
                    Route::delete('/:id$', 'index/MessagesNotice/removeNoticeMessage');
                })->middleware(CheckAuth::class, 'messages.notice');
            });
        });
        // 导入导出
        Route::group('export', function () { // 无UI
            Route::get('/$', 'index/Export/getAllLogs'); // 获取导出记录
            Route::post('/', 'index/Export/extractQuestions'); // 导出题目
            Route::get('/:code$', 'index/Export/downloadZip'); // 下载导出的文件
            Route::delete('/:id', 'index/Export/withdrawExtract'); // 撤销导出
        })->middleware(CheckAuth::class, 'questions.export');
    })->middleware(CheckAuth::class, CheckAuth::LOGIN)->pattern('id', '\d+');;
});

Route::get('413', 'index/Index/e413');
Route::miss('index/Index/e404');
